# 通过模板生成文档
import os

from docx import Document

infos = [
    ['京A88888', '2003', '12','12', '12', '34', '200', '闯红灯'],
    ['京A99999', '2013', '1', '12', '12', '34', '200', '非法占用非机动车道'],
    ['京A520JX', '2023', '2', '12', '12', '34', '200', '违章停车'],
    ['京A00000', '2023', '4', '12', '12', '34', '200', '不按交通指示路线行驶'],
]

for info in infos:
    doc1 = Document('../02.py_wordFile/05.违章通知书模板-通过模板生成文档.docx')
    for p in doc1.paragraphs:
        for run in p.runs:
            run.text = run.text.replace('{0}', info[0])
            run.text = run.text.replace('{1}', info[1])
            run.text = run.text.replace('{2}', info[2])
            run.text = run.text.replace('{3}', info[3])
            run.text = run.text.replace('{4}', info[4])
            run.text = run.text.replace('{5}', info[5])
            run.text = run.text.replace('{6}', info[6])
            run.text = run.text.replace('{7}', info[7])

        # 注意直接修改段落内容，会丢失样式
        # p.text = p.text.replace('{0}', info[0])
        # p.text = p.text.replace('{1}', info[1])
        # p.text = p.text.replace('{2}', info[2])
        # p.text = p.text.replace('{3}', info[3])
        # p.text = p.text.replace('{4}', info[4])
        # p.text = p.text.replace('{5}', info[5])
        # p.text = p.text.replace('{6}', info[6])
        # p.text = p.text.replace('{7}', info[7])

    if not os.path.exists('../02.py_wordFile/违章通知'):
        os.mkdir('../02.py_wordFile/违章通知')
    doc1.save(f'../02.py_wordFile/违章通知/05.{info[0]}违章通知书.docx')

